﻿using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace System.Web.Mvc
{
    public static class ObjectExtensions
    {
        public static string ToJson(this object instance, IEnumerable<JavaScriptConverter> jsonConverters)
        {
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            jsonSerializer.RegisterConverters(jsonConverters ?? new JavaScriptConverter[0]);
            return jsonSerializer.Serialize(instance);
        }

        public static string ToJson(this object instance)
        {
            return instance.ToJson(null);
        }
    }
}